package com.wing.common.services;

import java.util.Iterator;

import javax.servlet.ServletConfig;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class ServiceManager extends AbstractServiceManager{
	
	public void init(ServletConfig aConfig) throws Exception {
		for (Iterator iterator = servicesList.iterator(); iterator.hasNext();) {
			String serviceBaseName = (String)iterator.next ();
			Class serviceBase = Class.forName (serviceBaseName);
			com.wing.common.services.Service service = (com.wing.common.services.Service) serviceBase.newInstance ();
			service.init(aConfig);
			cServicesMap.put(serviceBase, service);
		}
	}

	public void destroy(ServletConfig aConfig) {
		for (Iterator iterator = servicesList.iterator(); iterator.hasNext();) {
			com.wing.common.services.Service service = (com.wing.common.services.Service) iterator.next();
			try {
				service.destroy(aConfig);
			} catch (Exception exc) {
				exc.printStackTrace();
			}
		}

		cServicesMap.clear();

	}

	public static com.wing.common.services.Service getService(Class aClass) {
		return ((com.wing.common.services.Service) cServicesMap.get(aClass));
	}

}
